<template>
  <div class="server-main">
    <div class="server-table">
      <div class="table-title">
        <span class="table-title-text">报告生成</span>
      </div>
      <el-table :data="tableList" class="km-table">
        <el-table-column
          type="index"
          label="ID"
          width="50"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="name"
          label="测试对象名称"
          align="center"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          prop="createTime"
          label="下载时间"
          align="center"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          prop="createUseId"
          label="下载人员"
          align="center"
        ></el-table-column>
      </el-table>
      <el-pagination
        class="km-pagination"
        background
        :small="true"
        layout="total, prev, pager, next,sizes, jumper"
        :total="total"
        :hide-on-single-page="!total"
        :page-size.sync="modelData.pageSize"
        :pager-count="7"
        :page-sizes="[10, 20, 30]"
        :current-page="modelData.pageNum"
        @current-change="handleCurrentChange"
        @size-change="handleSizeChange"
      >
      </el-pagination>
    </div>
  </div>
</template>
<script>
import { mapState, mapMutations } from "vuex";
export default {
  name: "OntologyManagement",
  components: {},
  data: () => {
    return {
      modelData: {
        reportId: "",
        pageSize: 10,
      },
      tableList: [], // 下载记录列表
      total: 0, // 数据总条数
      loading: false,
      dialogFormVisible: false,
      uploadFormVisible: false,
      editOntologyId: null, // 编辑本体的id
      modelParams: {
        ontologyAllName: "", // 本体名称
        ontologyAllDescribe: "", // 本体描述
      },
      dialogType: "ADD",
      dialogConfig: {
        ADD: {
          title: "创建本体",
          funcName: "createOntology",
        },
        EDIT: {
          title: "编辑本体",
          funcName: "editOntology",
        },
      },
      rules: {
        ontologyAllName: [
          {
            required: true,
            message: "请输入本体名称",
            trigger: "blur",
          },
        ],
        ontologyAllDescribe: [
          {
            required: true,
            message: "请输入本体描述",
            trigger: "blur",
          },
        ],
      },
      uploadRules: {
        fileName: [
          {
            required: true,
            message: "请选择本体文件",
            trigger: ["change", "blur"],
          },
        ],
      },
      textSizeConfig: {
        minRows: 4,
        maxRows: 10,
      },
      uploadParams: {
        fileName: null,
      },
    };
  },
  computed: {
    ...mapState({
      containerDom: (state) => state.server.containerDom,
    }),
  },
  created() {
    this.modelData.reportId = this.$route.query.reportId;
    // 获取下载记录列表
    this.getModelList();
  },
  methods: {
    ...mapMutations({
      // save_allGraphData: "ontologyModel/save_allGraphData",
      save_ontologyAllId: "ontologyModel/save_ontologyAllId",
    }),
    // 获取下载报告列表
    getModelList() {
      let params = this.modelData;
      this.$axios
        .post(this.$api.resultAnalyse.zehaoObj.reportRecord, params)
        .then((res) => {
          let data = res.data.data.list;
          this.tableList = data;
          this.total = res.data.data.total;
        })
        .catch((err) => {
          console.error(err);
        });
    },
    // 修改当前页码
    handleCurrentChange(page) {
      this.modelData.pageNum = page;
      this.getModelList();
    },
    //修改当前页面容量
    handleSizeChange(page) {
      this.modelData.pageSize = page;
      this.getModelList();
    },
    // 跳转详情页
    goDetail(id) {
      this.$router.push(`/ontologyModel/ontologyManagement/edit/${id}`);
      this.save_ontologyAllId(id);
    },

    // 模态框关闭后
    afterDialogClose() {
      this.$refs.dialogForm && this.$refs.dialogForm.resetFields();
      this.$refs.uploadForm && this.$refs.uploadForm.resetFields();
    },
    // 打开新增本体框
    openCreateOntology() {
      this.dialogType = "ADD";
      this.dialogFormVisible = true;
    },
    // 取消
    resetDialogForm() {
      this.dialogFormVisible = false;
      this.uploadFormVisible = false;
    },
    // 确定
    submitDialogForm() {
      this.$refs.dialogForm.validate((valid) => {
        if (valid) {
          console.log("param", JSON.stringify(this.modelParams));
          let func = this.dialogConfig[this.dialogType].funcName;
          this[func]();
          this.dialogFormVisible = false;
        } else {
          return false;
        }
      });
    },
    // 创建本体
    createOntology() {
      let params = this.modelParams;
      console.log("add", JSON.stringify(params));
      this.loading = true;
      this.$axios
        .post(this.$api.ontologyModel.xinbiaoObj.createOntology, params)
        .then((res) => {
          let code = res.data.code;
          let ontologyAllId = res.data.data;
          if (+code === 10001) {
            this.$message.warning("当前本体名称已经存在！");
            this.loading = false;
            return;
          }
          this.loading = false;
          this.dialogFormVisible = false;
          // 跳转可视化界面
          this.goDetail(ontologyAllId);
          // this.onSubmit(1);
        })
        .catch((error) => {
          this.loading = false;
        });
    },
    // 编辑本体
    editOntology() {
      let params = {
        ontologyAllId: this.editOntologyId,
        ...this.modelParams,
      };
      // console.log('edit', params)
      this.loading = true;
      this.$axios
        .post(this.$api.ontologyModel.xinbiaoObj.editOntology, params)
        .then((res) => {
          let code = res.data.code;
          if (+code === 10001) {
            this.$message.warning("当前本体名称已经存在！");
            this.loading = false;
            return;
          }
          this.loading = false;
          this.dialogFormVisible = false;
          this.onSubmit();
        })
        .catch((error) => {
          this.loading = false;
        });
    },
    // 下载模板
    downloadFile() {
      this.$axios
        .get(this.$api.ontologyModel.xinbiaoObj.downloadModel)
        .then((res) => {
          let data = res.data.data;
          var iframe = document.createElement("iframe");
          iframe.style.display = "none";
          iframe.src = data;
          document.body.appendChild(iframe);
        })
        .catch((err) => {
          console.log(err);
        });
    },
    // 批量导入
    uploadModel(row) {
      this.uploadFormVisible = true;
      this.editOntologyId = row.ontologyAllId;
    },
    beforeUpload(file) {
      this.uploadParams.fileName = file.name;
      this.uploadFile = file;
      return false;
    },
    submitUploadForm() {
      this.loading = true;
      this.$refs.uploadForm.validate((valid) => {
        if (valid) {
          let params = {
            file: this.uploadFile,
            ontologyAllId: this.editOntologyId,
          };
          this.$fileHttp
            .uploadFile(this.$api.ontologyModel.xuekuiObj.uploladModel, params)
            .then((res) => {
              let code = res.data.code;
              if (code !== 200) {
                let message = res.data.message;
                this.$message.warning(message);
              }
              this.loading = false;
              this.uploadFormVisible = false;
            });
        } else {
          this.loading = false;
          return false;
        }
      });
    },
  },
};
</script>
<style lang="scss" scoped>
.server-main {
  position: relative;

  .search-form {
    background: #ffffff;
    border-radius: 4px;
    padding: 16px 32px;
    .el-form-item {
      margin-bottom: 0;
    }

    .form-input {
      margin-right: 32px;
      .el-input,
      .el-select {
        ::v-deep .el-input__inner {
          width: 240px;
          height: 32px;
          background: #ffffff;
          border-radius: 2px;
          border: 1px solid #e0e0e0;
        }
      }
    }
  }

  .server-table {
    margin-top: 16px;
    padding: 16px 32px 64px;
    background: #ffffff;
    border-radius: 4px;

    .table-title {
      display: flex;
      justify-content: space-between;
      align-items: center;

      .table-title-text {
        margin: 8px 0;
        font-size: 16px;
        line-height: 16px;
        border-left: 5px solid $--color-primary;
        padding-left: 10px;
      }

      margin-bottom: 10px;
    }
  }

  .ontology-edit {
    padding: 0 10px;
    & + .ontology-edit {
      border-left: 1px solid #dddddd;
    }
  }

  ::v-deep .el-table__append-wrapper {
    padding: 10px;
  }

  ::v-deep .el-dialog {
    width: 500px;
    border-radius: 5px;
  }

  .dialog-inline {
    ::v-deep .el-form-item__content {
      width: 300px;
    }

    button.el-button {
      background-color: $--color-primary !important;
      color: #fff !important;
      line-height: 1.2;
    }
  }
  .upload-hint {
    font-size: 12px;
    color: #aaa;
    text-align: center;
    line-height: 40px;
  }
}
</style>
